package com.demo.androidgame.samgukji_hit;

import javax.microedition.khronos.opengles.GL10;

import com.demo.androidgame.fx.gl.Animation;
import com.demo.androidgame.fx.gl.Camera2D;
import com.demo.androidgame.fx.gl.SpriteBatcher;
import com.demo.androidgame.fx.gl.TextureRegion;
import com.demo.androidgame.fx.impl.GLGraphics;

public class MusicStageRender{

	SpriteBatcher batcher;
	GLGraphics glGraphics;
	Camera2D guiCam;
	MusicStageUpdate musicStageUpdate;
	Bow01 bow01;
	MusicUnit musicUnit;
	public float bowX, unitX;
	
	float stateTime;
	
	public MusicStageRender(SpriteBatcher batcher, GLGraphics glGraphics, MusicStageUpdate musicStageUpdate){
		guiCam = new Camera2D(glGraphics, 800, 480);
		this.glGraphics = glGraphics;
		this.batcher = batcher;
		this.musicStageUpdate = musicStageUpdate;
		
		unitX = 30;
		stateTime = 0;
	}
	
	public void render(float deltaTime){
		guiCam.setViewportAndMatrices();
		renderBackground();
		renderObject(deltaTime);
//		renderMusicUnit(stateTime);
	}
	
	public void renderObject(float deltaTime){
		GL10 gl = glGraphics.getGL();
        gl.glEnable(GL10.GL_BLEND);
        gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
        
        batcher.beginBatch(Assets.mBow01);
        renderBow01();        
        batcher.endBatch();
        
        batcher.beginBatch(Assets.mMusicUnit);
        renderMusicUnit(deltaTime);        
        batcher.endBatch();
        
        gl.glDisable(GL10.GL_BLEND);
	}
	
	public void renderBow01(){
		TextureRegion keyFrame;
		int len = musicStageUpdate.bows01.size();
		for(int i = 0; i < len; i++){
			bow01 = musicStageUpdate.bows01.get(i);
			keyFrame = Assets.mBowAni01.getKeyFrame(bow01.bow01StateTime, Animation.ANIMATION_LOOPING);
			batcher.drawSprite(bow01.position.x, bow01.position.y, 55, 12, keyFrame);
			
			bow01.position.x += 4;
		}
	}
	
	public void renderBackground(){
		batcher.beginBatch(Assets.mMusicBack);
        batcher.drawSprite(400, 240, 800, 480, Assets.mMusicBackRegion);
        batcher.endBatch();
	}
	
	public void renderMusicUnit(float deltaTime){
		TextureRegion keyFrame;
//		batcher.beginBatch(Assets.mMusicUnit);
		int len = musicStageUpdate.musicUnits.size();
		for(int i = 0; i < len; i++){
			if(i < 5){
			musicUnit = musicStageUpdate.musicUnits.get(i);
			keyFrame = Assets.mMusicUnitAni.getKeyFrame(stateTime, Animation.ANIMATION_LOOPING);
			batcher.drawSprite(musicUnit.position.x, musicUnit.position.y- (40*i), 98, 74, keyFrame);
			}
		}
		
//		batcher.endBatch();
	}

}
